package org.example.web.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.Main;
import org.example.util.JwtUtils;
import org.example.web.domain.Emp;
import org.example.web.domain.Result;
import org.example.web.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class LoginController {
    @Autowired
    private EmpService empService;
    @Autowired
    private JwtUtils jwtUtils;


    @PostMapping("/login")
    public Result login(@RequestBody Emp emp) {
        Emp e = empService.getUserByUserNameAndPassword(emp);
        log.info("emp = {}",emp);
        if (e == null) {
            return Result.notLogin();
        }else{
            Map<String, Object> map = new HashMap<>();
            map.put("username", e.getUsername());
            map.put("id", e.getId());
            map.put("gender", e.getGender());
            String token = jwtUtils.generateJwt(map);
            return Result.success(token);
        }

    }
}
